using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using SurveyHost.Core.DAL;
using SurveyHost.Core;

namespace SurveyHost.Web.Admin
{
    public partial class EditParagraph : System.Web.UI.Page
    {
        #region  Properties and variables (3)

        protected string LastUrl
        {
            get
            {
                return (string)ViewState["LastUrl"];
            }
            set
            {
                ViewState["LastUrl"] = value;
            }
        }
        protected string ContentName
        {
            get
            {
                return (string)ViewState["ContentName"];
            }
            set
            {
                ViewState["ContentName"] = value;
            }
        }

        #endregion

        #region  Methods (3)

        protected void Cancel_Click(object sender, EventArgs e)
        {
            if (LastUrl != null)
                Response.Redirect(LastUrl, true);
            TextEditor.Text = string.Empty;
            ResultMessage.Text = "Cancelled";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request == null || Request.UrlReferrer == null)
                    Response.Redirect(ResolveUrl("~/default.aspx"),true);
                LastUrl = Request.UrlReferrer.ToString();
                ContentName = SubSonic.Utilities.Utility.GetParameter("id");
                if (ContentName != string.Empty)
                {
                    Content text = new Content(Content.Columns.ContentName, ContentName);
                    if (text.Body != null)
                    {
                        TextEditor.Text = text.Body;
                    }
                }
            }
        }

        protected void Submit_Click(object sender, EventArgs e)
        {

            Content text = new Content(Content.Columns.ContentName, ContentName);
            if (!text.IsLoaded)
            {
                text.IsNew = true;
                text.ContentGUID = Guid.NewGuid();
            }

            text.ContentName = ContentName;
            text.Body = HttpUtility.HtmlEncode(TextEditor.Text);
            text.Save(WebUtil.CurrentUserId());
            Response.Redirect(LastUrl);
        }

        #endregion 
    }
}
